package net.starelement.gamelib.game;

import cn.nukkit.Server;
import cn.nukkit.event.EventHandler;
import cn.nukkit.event.Listener;
import cn.nukkit.event.player.PlayerJoinEvent;
import cn.nukkit.event.player.PlayerQuitEvent;
import cn.nukkit.scheduler.ServerScheduler;
import net.starelement.gamelib.GameLib;
import net.starelement.gamelib.player.PlayerList;

/**  StarElement 游戏准备类
 *
 * @author Ruok233
 * @since NukkitX API 1.0.9 | StarElement Project v1-alpaca
 *
 * create: 2019/12/10
 */

public abstract class GameReady implements Listener {

    public Server server = Server.getInstance();
    public ServerScheduler scheduler = server.getScheduler();
    public PlayerList playerList = PlayerList.getInstance();
    public int count = 2;
    public static boolean interact = true;

    public int team;
    //35 7 14;35 7 -5

    public GameReady() {
        //实例化时创建监听器
        server.getPluginManager().registerEvents(this, GameLib.getInstance());
    }

    /**
     * @param count 开局人数
     */
    public GameReady(int count) {
        this();
        this.count = count;
    }

    //玩家加入事件
    @EventHandler
    public void playerJoin(PlayerJoinEvent event) {
        int size = server.getOnlinePlayers().size();
        playerList.add(event.getPlayer());
        //判断游戏是否已开局
        if (!GameBase.getState()) {
            event.setJoinMessage("§f玩家 §e" + event.getPlayer().getName() + " §f加入了游戏！§b (" + size + "/" +  count + ")");
        } else {
            event.setJoinMessage("§f玩家 §e" + event.getPlayer().getName() + " §f加入了游戏！");
            event.getPlayer().sendMessage("§b§l游戏已开局，进入§e观战模式");
        }
    }

    //玩家离开事件
    @EventHandler
    public void playerQuit(PlayerQuitEvent event) {
        playerList.remove(event.getPlayer());
        event.setQuitMessage("§f玩家 §e" + event.getPlayer().getName() + " §f离开了");
        if (server.getOnlinePlayers().size() < count) {

        }
    }


    public abstract void run();
}
